import { connection } from '../app/database/mysql';
import { findFileById } from '../car/car.service';
import { getInfoByIdAndType} from '../corner/common.service'

/**
 * 获取内容
 */
export const getIndex = async (status: number) => {
  // 准备查询
  let shopStatement = `
    SELECT * FROM shop WHERE recommend = 1 order by sortNum
  `;
  let houseStatement = `
    SELECT * FROM house WHERE recommend = 1 order by sortNum
  `;
  let carStatement = `
    SELECT * FROM car WHERE recommend = 1 order by sortNum
  `;
  let jobsStatement = `
    SELECT * FROM jobs WHERE recommend = 1 order by sortNum
  `;
  // if (status >= 0) {
  //   statement += `where status = ` + status
  // }
  
  // 执行访问查询
  const [shop] = await connection.promise().query(shopStatement);

  // 执行访问查询
  const [jobs] = await connection.promise().query(jobsStatement);

  // 执行访问查询
  const [car] = await connection.promise().query(carStatement);

  // 执行访问查询
  const [houe] = await connection.promise().query(houseStatement);

  // 提供数据
  return { shop, jobs, car, houe };
}

/**
 * 按 ID 获取内容
 */
export const getIdIndex = async (postId: number) => {
  // 准备查询
  const statement = `
    SELECT * FROM house
    WHERE id = ?
  `;

  // 执行查询
  const [data] = await connection.promise().query(statement, postId);

  // 提供数据
  return data[0];
}

/**
 * 获取信息圈内容
 */
export const corner = async (userId: number, status: number, pageNo: number, pageSize: number) =>{
  let statement = `
    SELECT SQL_CALC_FOUND_ROWS * FROM tb_index 
  `;
  let where = []
  if (userId>0) {
    where.push(`user_id = `+userId)
  }
  if (status >= 0) {
    where.push(`status = ` + status) 
  }

  if (where.length > 0) {
    statement += ` where `
    for(let i = 0; i < where.length; i++) {
      statement += where[i]
      if (i<where.length-1) {
        statement += ` and `
      }
    }
  }
  
  
  statement += ` order by id desc limit ` + (pageNo > 0 ? (pageNo - 1) * pageSize : 0) + `,` + pageSize

  // 执行查询
  const [data] = await connection.promise().query(statement);
  let totalStatement = `SELECT FOUND_ROWS() as total`
  const [result] = await connection.promise().query(totalStatement)
  let totalCount = result[0].total
  let totalPage = (totalCount % pageSize) > 0 ? parseInt(`${totalCount / pageSize}`) + 1 : totalCount / pageSize

  let arr = []
  for(let item of data as any){
    let info = await getInfoByIdAndType(item.target_id, item.type)
    if (!info) continue
    item.data = info;
    arr.push(item)

    if (item.data.image_id && item.data.image_id > 0) {

      let cover = await findFileById(item.data.image_id, 0, 0, 'medium');
      item.cover = cover[0];
    }
  }

  let res = {
    data:arr,
    totalPage,
    totalCount
  }

  return res;
}

/**
 * 更改信息圈内容状态
 */
export const changeStatus = async (post: any, type: number, target_id: number) => {
  // 准备查询
  const statement = `
    UPDATE tb_index
    SET ?
    WHERE type = ? AND target_id = ?;
  `;

  // 执行查询
  const [data] = await connection.promise().query(statement, [post, type, target_id]);

  // 提供数据
  return data[0];
}
